<template>
    <div class="my-container">
      <!-- 未登录 -->
      <div class="not-login" v-if="!user">
        <div class="login-box" @click="goLogin">
          <img src="../../assets/mobile.png" alt="">
          <span>登录 / 注册</span>
        </div>
      </div>
      <!-- 已登录 -->
      <div class="isLogin" v-else>
        <div class="isLogin-top">
          <span class="isLogin-top-left">
            <van-image
            round
            fit="cover"
            :src='userInfo.photo'/>{{userInfo.name}}</span>
          <span class="isLogin-top-right">
            <van-button round type="default">编辑资料</van-button>
          </span>
        </div>
        <div class="isLogin-buttom">
          <div class="isLogin-buttom-item">
            <span>{{userInfo.art_count}}</span>
            <span>头条</span>
          </div>
          <div class="isLogin-buttom-item">
            <span>{{userInfo.follow_count}}</span>
            <span>关注</span>
          </div>
          <div class="isLogin-buttom-item">
            <span>{{userInfo.fans_count}}</span>
            <span>粉丝</span>
          </div>
          <div class="isLogin-buttom-item">
            <span>{{userInfo.like_count}}</span>
            <span>获赞</span>
          </div>
        </div>
      </div>

      <!-- 收藏 历史 -->
      <van-grid :column-num="2" class="my-grid" clickable :border="false">
        <van-grid-item>
          <i slot="icon" class="iconfont iconshoucang sc"></i>
          <span slot="text">收藏</span>
        </van-grid-item>
        <span class="borderBox"></span>
        <van-grid-item>
          <i slot="icon" class="iconfont iconlishi ls"></i>
          <span slot="text">历史</span>
        </van-grid-item>
      </van-grid>
      <van-cell-group class="mid-cell">
        <van-cell title="消息通知" is-link />
        <van-cell title="小智障同学" is-link />
      </van-cell-group>
      <van-cell-group class="botton-cell" v-if="user">
        <van-cell clickable title="退出登录" @click="loginOut"/>
      </van-cell-group>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'my',
  data () {
    return {
      userInfo: {
      }
    }
  },

  created () {
    // 如果没有token不触发该函数
    if (this.user) {
      this.loadUserInfo()
    }
  },
  // mounted () {
  //   this.xxx()
  // },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // xxx () {
    //   window.localStorage.setItem('token', '213')
    // },
    // 跳转到登录页
    goLogin () {
      this.$router.push('/login')
    },
    // 点击退出登录
    loginOut () {
      this.$dialog.confirm({
        title: '头条',
        message: '退出登录?'
      })
      // 点击确认
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        // 点击取消
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        // console.log(res)
        this.userInfo = data
      } catch (err) {
        console.dir(err)
        if (err.response.status === 401) {
          this.$toast.fail('token已过期')
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
.my-container {
  .not-login {
    width: 100%;
    height: 401px;
    background: url('../../assets/banner.png');
    background-size: cover;
    display: flex;
    align-items: center;
    justify-content: center;
    .login-box {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      span {
        font-size: 28px;
        color: white;
      }
    }
  }
  .isLogin {
    background: url('../../assets/banner.png');
    background-size: cover;
    .isLogin-top {
      height: 231px;
      // background: pink;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      padding: 60px 32px 0px 32px;
      .isLogin-top-left {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 30px;
        color: white;
        .van-image {
          width: 132px;
          height: 132px;
          margin-right: 22px;
          border: 2px solid white;
        }
      }
      .isLogin-top-right {
        display: flex;
        align-items: center;
        .van-button {
          white-space: nowrap;
          width: 115px;
          height: 32px;
          font-size: 20px;
          color: #666666;
          // overflow: hidden;
        }
      }
    }
    .isLogin-buttom {
      height: 170px;
      // background: blue;
      display: flex;
      justify-content: space-around;
      .isLogin-buttom-item {
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        color: white;
        span:nth-child(1) {
          font-size: 36px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
        }
    }
  }
  .my-grid {
    position: relative;
    .van-grid-item {
      height: 141px;
      .sc {
        font-size: 45px;
        color: #EB5253;
      }
      .ls {
        font-size: 45px;
        color: #FF9D1D;
      }
      span {
        font-size: 28px;
        letter-spacing: 20px;
        margin-top: 10px;
        margin-left: 16px;
      }
    }
    .borderBox {
      width: 2px;
      height: 102px;
      background: #EDEFF3;
      position: absolute;
      left: 374px;
      margin-top: 22px;
    }
  }
  .mid-cell {
    font-size: 30px;
    margin: 9px 0;
    .van-cell {
      height: 100px;
    }
  }
  .botton-cell {
    font-size: 30px;
    .van-cell {
      height: 104px;
    }
    .van-cell__title {
      color: #D86262;
      text-align: center;
    }
  }
}
</style>
